package com.ydd.moudle.home;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.taofengsdk.adutil.Utils;
import com.ydd.R;
import com.ydd.adapter.PhotoViewPagerAdapter;
import com.ydd.base.BaseActivity;
import com.ydd.share.ShareDialog;
import com.ydd.util.Util;
import com.ydd.util.glide.DownLoadImageHelper;
import com.ydd.util.glide.DownLoadUntils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cn.sharesdk.framework.Platform;

/**
 * Created by tao on 17-3-18.
 *
 * @function 显示大图
 */

public class PhotoViewActivity extends BaseActivity implements View.OnClickListener {
    public static final String PHOTO_LIST = "photo_list";
    /**
     * UI
     */
    private ViewPager mViewPager;
    private TextView mIndictorView;
    private ImageView mShareView;
    private ImageView mDownView;
    /**
     * Data
     */
    private List<String> mPhotoLsit;
    private int mLength;
    private PhotoViewPagerAdapter mPagerAdapter;
    private int currentPos;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_view_layout);

        initData();
        initView();
    }

    private void initData() {
        mPhotoLsit = new ArrayList<>();
        mPhotoLsit = getIntent().getStringArrayListExtra(PHOTO_LIST);
        mLength = mPhotoLsit.size();
    }

    private void initView() {
        mViewPager = (ViewPager) findViewById(R.id.photo_pager);
        mIndictorView = (TextView) findViewById(R.id.indictor_view);
        mShareView = (ImageView) findViewById(R.id.share_view);
        mDownView = (ImageView) findViewById(R.id.down_view);
        mViewPager.setPageMargin(Utils.dip2px(this, 30));
        mShareView.setOnClickListener(this);
        mDownView.setOnClickListener(this);
        mPagerAdapter = new PhotoViewPagerAdapter(this, mPhotoLsit, false);
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mIndictorView.setText(String.valueOf((position + 1)).concat("/").
                        concat(String.valueOf(mLength)));
                currentPos = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        Util.hideSoftInputMethod(this, mIndictorView);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.share_view:
                ShareDialog dialog = new ShareDialog(this, true);
                dialog.setShareType(Platform.SHARE_IMAGE);
                dialog.setShareTitle(getString(R.string.story));
                dialog.setShareTitleUrl(getString(R.string.story_site));
                dialog.setShareText(getString(R.string.story));
                dialog.setShareSite(getString(R.string.story));
                dialog.setShareTitle(getString(R.string.story));
                dialog.setImagePhoto(mPhotoLsit.get(currentPos));
                dialog.setUrl(mPhotoLsit.get(currentPos));
                dialog.setResourceUrl(mPhotoLsit.get(currentPos));
                dialog.show();
                break;

            case R.id.down_view:
                DownLoadUntils.onDownLoadImage(this, mPhotoLsit.get(currentPos),
                        new DownLoadImageHelper.ImageDownLoadCallBack() {

                            @Override
                            public void onDownLoadSuccess() {
                                Toast.makeText(getApplicationContext(), getString(R.string.download_success),
                                        Toast.LENGTH_SHORT)
                                        .show();
                            }


                            @Override
                            public void onDownLoadFailed() {
                                Toast.makeText(getApplicationContext(), getString(R.string.download_failed),
                                        Toast.LENGTH_SHORT)
                                        .show();
                            }
                        });
                break;
            default:
                break;
        }
    }
}
